package org.test.once.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.test.once.model.Module;
import org.test.once.manager.ModuleManager;

/**
 * 
 * @author cennanfang
 * @date 2013-7-22
 * @fileName UserServlet.java
 */
public class ModuleServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("GB2312");

		ModuleManager moduleManager = new ModuleManager();
		String command = req.getParameter("command");
		System.out.println(command);
		/**
		 * 
		 */
		if ("search".equals(command)) {
			List <Module> modules=new ArrayList<Module>();
		   String roleid=req.getParameter("roleid");
				// 将用户添加到session中
			  modules=moduleManager.searchModule();
	          req.setAttribute("modules", modules);
	 if(roleid!=null||"".equals(roleid)){
		 
				 req.setAttribute("roleid",roleid);
				 req.getRequestDispatcher("grant/role_module.jsp").forward(req, resp); 
			 }else{
			 req.getRequestDispatcher("module/module_list.jsp").forward(req, resp);
			 }
		 }else if("delModule".equals(command)){
			 int id=Integer.parseInt(req.getParameter("id"));
			 moduleManager.delModule(id);
		
			 req.getRequestDispatcher("common/pub_del_success.jsp").forward(req, resp);
			
	
				
	
		}else if("modifyModule".equals(command)){
			Module module=null;
			String name = req.getParameter("name");
			int pid = Integer.parseInt(req.getParameter("pid"));
			String url = req.getParameter("url");
			int id= Integer.parseInt(req.getParameter("id"));
		    System.out.println("123"+pid);
		    System.out.println("123"+name);
			module= new Module();
			module.setId(id);
			//System.out.println("执行了");
			module.setName(name);
			module.setPid(pid);
		    module.setUrl(url);
		    System.out.println("345"+module.getName());
		    moduleManager.updateModule(module);
			System.out.println("执行了");
			req.getRequestDispatcher("common/pub_update_success.jsp").forward(
					req, resp);
			
			
		}else if("addModule".equals(command)){
			String name=req.getParameter("name");
			int pid=Integer.parseInt(req.getParameter("pid"));
			String url=req.getParameter("url");
			Module module=new Module();
			module.setName(name);
			module.setPid(pid);
			module.setUrl(url);
			 System.out.println("123"+pid);
			    System.out.println("123"+name);
			moduleManager.addModule(module);
			System.out.println("dajiahao");
			req.getRequestDispatcher("common/pub_add_success.jsp").forward(req, resp);
		}else if("findByid".equals(command)){
			 System.out.println(command);
			 int id=Integer.parseInt(req.getParameter("id"));
			 System.out.println(id);
			 Module module=moduleManager.findModuleById(id);
			 req.setAttribute("module", module); 
			 req.getRequestDispatcher("module/modify.jsp").forward(req, resp); 
			
		}else if("findChild".equals(command)){
			System.out.println("234");
			List <Module> modules=new ArrayList<Module>();
			int id=Integer.parseInt(req.getParameter("id"));
			 System.out.println(id);
			 modules=moduleManager.findChildModule(id);
			  req.setAttribute("modules", modules);
			 req.getRequestDispatcher("module/result_list.jsp").forward(req, resp);
		}
		

}

}
